package main

import (
	"flag"
	"fmt"
	"github.com/gin-gonic/gin"
	"github.com/vueadmin/app/cron/internal/config"
	"github.com/vueadmin/app/cron/internal/cron"
	"github.com/vueadmin/app/cron/internal/svc"
	"github.com/vueadmin/middleware"
	"github.com/vueadmin/utils"
	"net/http"
	"os"
)

func main() {
	var configFile = flag.String("f", "etc/config.yaml", "the config file")
	flag.Parse()
	var c config.Config
	utils.MustLoad(*configFile, &c)
	gin.SetMode(c.Mode)

	svcCtx := svc.NewServiceContext(c) //注册服务

	var r = gin.New()
	r.Use(middleware.RecoverMiddleware(svcCtx.Log))

	r.GET(fmt.Sprintf("/%s/health", c.Prefix), func(c *gin.Context) {
		c.String(http.StatusOK, "ok")
	})

	server := &http.Server{
		Addr:    fmt.Sprintf(":%d", c.Port),
		Handler: r,
	}

	cron.StartCron(svcCtx) //启动定时任务

	fmt.Printf("Starting server at port:%d...\n", c.Port)
	if err := server.ListenAndServe(); err != nil {
		fmt.Println("启动失败：", err)
		os.Exit(1)
	}
}
